package connect4swt.gui;

import com.swtdesigner.*;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import connect4swt.ClientApplication;
import connect4client.core.Controller;
import connect4client.net.ConnectionController;
import connect4client.net.servers.Server;


/**
 * @author  Joel Cordeiro
 */
public class RegisterWindow {

	private Vector<String> socketServers;
	private Vector<String> rmiServers;
	public Object open() {
		final Display display = Display.getDefault();

		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	protected void createContents() {
		shell = new Shell(SWT.DIALOG_TRIM);
		shell.setImage(SWTResourceManager.getImage(LoginWindow.class, "/images/connect4Icon.ico"));
		shell.setSize(305, 238);
		shell.setText("Register");

		cmbServer = new Combo(shell, SWT.NONE);
		cmbServer.setBounds(83, 32, 211, 21);
		cmbServer.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event) {						
						cmbServerIp.removeAll();
		        	  	setCmbIP(cmbServer.getText());
		      }
		});

		cmbServerIp = new Combo(shell, SWT.NONE);
		cmbServerIp.setBounds(83, 62, 211, 21);

		txtLogin = new Text(shell, SWT.BORDER);
		txtLogin.setBounds(83, 90, 211, 21);

		txtPassword = new Text(shell, SWT.BORDER);
		txtPassword.setEchoChar('*');
		txtPassword.setBounds(83, 116, 211, 21);

		txtConfirmPassword = new Text(shell, SWT.BORDER);
		txtConfirmPassword.setBounds(83, 143, 211, 21);
		txtConfirmPassword.setEchoChar('*');

		final Button btnOK = new Button(shell, SWT.NONE);
		btnOK.setText("OK");
		btnOK.setBounds(161, 169, 60, 28);
		btnOK.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent arg0) {				
				if(txtLogin.getText().length() == 0 || txtPassword.getText().length() == 0 || txtConfirmPassword.getText().length() == 0){					

					MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
					msgBox.setText("Warning");
					msgBox.setMessage("Please insert Login/Password!");
					msgBox.open();
					return;
				}	
				else{
					if(!txtPassword.getText().equals(txtConfirmPassword.getText())){
						MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
						msgBox.setText("Warning");
						msgBox.setMessage("The passwords must be equal!");
						msgBox.open();
						return;						
					}

					sendRegister(txtLogin.getText(),txtPassword.getText());
				}
			}
		});

		final Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent arg0) {
				shell.dispose();
			}
		});
		btnCancel.setBounds(226, 169, 70, 28);
		btnCancel.setText("Cancel");

		final Label lblLogin = new Label(shell, SWT.NONE);
		lblLogin.setText("Login:");
		lblLogin.setBounds(41, 93, 38, 16);

		final Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setBounds(18, 119, 61, 16);
		lblPassword.setText("Password:");

		final Label lblServer = new Label(shell, SWT.NONE);
		lblServer.setBounds(32, 35, 47, 16);
		lblServer.setText("Server:");
				
		final Label lblServerIp = new Label(shell, SWT.NONE);
		lblServerIp.setBounds(32, 65, 47, 16);
		lblServerIp.setText("Server Ip:");

		final Label lblHeader = new Label(shell, SWT.NONE);
		lblHeader.setImage(SWTResourceManager.getImage(RegisterWindow.class, "/images/reg.png"));
		lblHeader.setBounds(0, 0, 300, 26);

		final Label lblConfirmPassword = new Label(shell, SWT.WRAP);
		lblConfirmPassword.setBounds(18, 139, 61, 31);
		lblConfirmPassword.setText("Confirm password:");

		this.initializeFields();	
	}

	/**
	 * @return
	 * @uml.property  name="instance"
	 */
	public static RegisterWindow getInstance(){
		return instance;
	}

	private void initializeFields(){

		cmbServer.add("Socket");
		cmbServer.add("RMI");
		cmbServer.select(1);

		this.socketServers = new Vector<String>();		
		this.socketServers.add("127.0.0.1");
		this.socketServers.add("192.168.1.4");

		this.rmiServers = new Vector<String>();	
		this.rmiServers.add("127.0.0.1");
		this.rmiServers.add("192.168.1.3");	
		
		setCmbIP(cmbServer.getText());
	}

	private void setCmbIP(String type){
		if(type.equals("Socket")){	    	
			for(String text : socketServers){
				cmbServerIp.add(text);		        	    		
			}
		}
		else if(type.equals("RMI")){
			for(String text : rmiServers){
				cmbServerIp.add(text);	    		
			}
		}		        	    
		cmbServerIp.select(0);
	}

	private void sendRegister(String login, String password) {
		Server server = null;
		ConnectionController.CommunicationMethod method = null;
		if (cmbServer.getText().equals("RMI")) {
			server = new Server(cmbServerIp.getText(), 1099);
			method = ConnectionController.CommunicationMethod.RMI;
		} else if (cmbServer.getText().equals("Socket")) {
			server = new Server(cmbServerIp.getText(), 32000);
			method = ConnectionController.CommunicationMethod.TCP;
		}

		Controller controller = ClientApplication.getController();
		controller.getGUIMediator().sendRegisterMessage(method, server, txtLogin.getText(), txtPassword.getText());
	}	
	
	public void showRegistrationFailed() {		
		shell.getDisplay().syncExec(new Runnable() {
			public void run() {
				System.err.println(isDisposed());
				
				MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
				msgBox.setText("Warning");
				msgBox.setMessage("Registration failed!");
				msgBox.open();
			}
		});
	}

	public void dispose() {
		if (!isDisposed()) {
			shell.getDisplay().syncExec(new Runnable() {
				public void run() {
					shell.dispose();
				}
			});
		}
	}


	public boolean isDisposed() {
		if (shell == null)
			return true;

		return shell.isDisposed();

	}
	
	private RegisterWindow(){}

	private static RegisterWindow instance = new RegisterWindow();
	private Text txtConfirmPassword;
	private Combo cmbServerIp;
	private Combo cmbServer;
	private Text txtLogin;
	private Text txtPassword;
	private Object result;
	private Shell shell;
}
